import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:scan/scanLine.dart';
import 'package:scan/scannerOverlayShape.dart';

class Scan {
  static const MethodChannel _channel = const MethodChannel('chavesgu/scan');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<String?> parse(String path) async {
    final String? result = await _channel.invokeMethod('parse', path);
    return result;
  }
}

class ScanView extends StatefulWidget {
  ScanView(
      {this.controller,
      this.onCapture,
      this.scanLineColor = Colors.green,
      required this.overlay});

  final ScanController? controller;
  final CaptureCallback? onCapture;
  final Color scanLineColor;
  final ScanOverlayShape overlay;

  @override
  State<StatefulWidget> createState() => _ScanViewState();
}

class _ScanViewState extends State<ScanView> {
  MethodChannel? _channel;

  Widget _getPlatformView(BuildContext context) {
    if (Platform.isIOS) {
      return UiKitView(
        viewType: 'chavesgu/scan_view',
        creationParamsCodec: StandardMessageCodec(),
        creationParams: {
          "areaWidth": widget.overlay.scanArea.width,
          "areaHeight": widget.overlay.scanArea.height,
          "bottomOffset": widget.overlay.bottomOffset
        },
        onPlatformViewCreated: (id) {
          _onPlatformViewCreated(id);
        },
      );
    } else {
      return AndroidView(
        viewType: 'chavesgu/scan_view',
        creationParamsCodec: StandardMessageCodec(),
        creationParams: {
          "areaWidth": widget.overlay.scanArea.width,
          "areaHeight": widget.overlay.scanArea.height,
          "bottomOffset": widget.overlay.bottomOffset
        },
        onPlatformViewCreated: (id) {
          _onPlatformViewCreated(id);
        },
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.center,
      children: [
        _getPlatformView(context),
        Container(
          decoration: ShapeDecoration(
            shape: widget.overlay,
          ),
        ),
        Container(
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: widget.overlay.bottomOffset * 2),
            child: Container(
              child: ScanLine(color: widget.scanLineColor),
              width: widget.overlay.scanArea.width,
              height: widget.overlay.scanArea.height,
            )),
      ],
    );
  }

  void _onPlatformViewCreated(int id) {
    _channel = MethodChannel('chavesgu/scan/method_$id');
    _channel?.setMethodCallHandler((MethodCall call) async {
      if (call.method == 'onCaptured') {
        if (widget.onCapture != null)
          widget.onCapture!(call.arguments.toString());
      }
    });
    widget.controller?._channel = _channel;
  }
}

typedef CaptureCallback(String data);

class ScanArea {
  const ScanArea(this.width, this.height);

  final double width;
  final double height;
}

class ScanController {
  MethodChannel? _channel;

  ScanController();

  void resume() {
    _channel?.invokeMethod("resume");
  }

  void pause() {
    _channel?.invokeMethod("pause");
  }

  void toggleTorchMode() {
    _channel?.invokeMethod("toggleTorchMode");
  }
}
